package cn.software.controller;

import cn.software.entity.Subject;
import cn.software.entity.param.SubjectParam;
import cn.software.entity.vo.SubjectVo;
import cn.software.service.SubjectService;
import cn.software.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/subject")
@Api(tags = "学科接口")
public class SubjectController {

    @Autowired
    private SubjectService subjectService;

    @PostMapping("/addSubject")
    @ApiOperation("增加学科")
    public Result addSubject(@RequestBody SubjectParam subjectParam) {
        boolean isSuccess = subjectService.addSubject(subjectParam);
        if(isSuccess) {
            return Result.success(null);
        } else {
            return Result.fail("失败");
        }
    }

    @PostMapping("/updateSubject")
    @ApiOperation("修改学科")
    public Result updateSubject(@RequestBody SubjectParam subjectParam){
        boolean isSuccess=subjectService.updateSubject(subjectParam);
        if(isSuccess) {
            return Result.success(null);
        } else {
            return Result.fail("失败");
        }
    }

    @PostMapping("/deleteSubject")
    @ApiOperation("删除学科")
    public Result deleteSubject(@RequestBody SubjectParam subjectParam){
        boolean isSuccess=subjectService.deleteSubject(subjectParam);
        if(isSuccess) {
            return Result.success(null);
        } else {
            return Result.fail("失败");
        }
    }

    @GetMapping("/getSubject")
    @ApiOperation("查询学科")
    public Result getSubject(){
        List<Subject> list=subjectService.getSubject();
        if(list!=null) {
            return Result.success(list);
        } else {
            return Result.fail("查询结果为空");
        }
    }

    @GetMapping("/getSubjectById")
    @ApiOperation("根据id查询学科信息")
    public Result getSubjectById(@RequestParam Integer id){
        SubjectVo subjectVo=subjectService.getSubjectById(id);
        if(subjectVo!=null){
            return Result.success(subjectVo);
        }else {
            return Result.fail("查询结果为空");
        }
    }

}
